﻿namespace com.vs.General.BL
{
    using com.vs.General;
    using System;
    using System.Text;

    public class LicenseParser
    {
        private string ReplaceLicenseKeyComponent(Session session, string licenseKey, string replacementValue, int componentPosition)
        {
            string str = null;
            StringBuilder builder = new StringBuilder();
            string[] strArray2 = StringManager.Split(licenseKey, new string[] { LicenseKeyConstants.stDODELIM });
            str = strArray2[componentPosition];
            string[] strArray = StringManager.Split(str, new string[] { LicenseKeyConstants.stCOLON });
            strArray[1] = replacementValue;
            strArray2[componentPosition] = strArray[0] + LicenseKeyConstants.stCOLON + strArray[1];
            for (int i = 0; i < strArray2.Length; i++)
            {
                if (i > 0)
                {
                    builder.Append(Constants.stDODELIM);
                }
                builder.Append(strArray2[i]);
            }
            return builder.ToString();
        }

        public string ReplaceLicenseKeyExpirationDate(Session session, string licenseKey, DateTime expirationDate)
        {
            return this.ReplaceLicenseKeyComponent(session, licenseKey, DateManager.ToStringWithInvariantCulture(expirationDate), LicenseKeyConstants.iKEY_EXPIRATION_DATE);
        }

        public string ReplaceLicenseKeyModules(Session session, string licenseKey, string newLicenseKey)
        {
            return this.ReplaceLicenseKeyComponent(session, licenseKey, newLicenseKey, LicenseKeyConstants.iKEY_MODULES);
        }

        public string rtrvBiosSerialNum(Session session, string stLicenseKey)
        {
            return this.rtrvLicenseComponent(stLicenseKey, LicenseKeyConstants.iKEY_BIOS_SERIAL_NUM);
        }

        public DateTime RtrvCreationDate(Session session, string stLicenseKey)
        {
            string str = this.rtrvLicenseComponent(stLicenseKey, LicenseKeyConstants.iKEY_CREATION_DATE);
            if (StringManager.IsEmpty(str))
            {
                return DateTime.MinValue;
            }
            return DateManager.ParseWithInvariantCulture(str);
        }

        public string rtrvExpirationAction(Session session, string stLicenseKey)
        {
            return this.rtrvLicenseComponent(stLicenseKey, LicenseKeyConstants.iKEY_EXPIRATION_ACTION);
        }

        public DateTime rtrvExpirationDate(Session session, string stLicenseKey)
        {
            DateTime time = new DateTime();
            return DateManager.ParseWithInvariantCulture(this.rtrvLicenseComponent(stLicenseKey, LicenseKeyConstants.iKEY_EXPIRATION_DATE));
        }

        private string rtrvLicenseComponent(string licenseKey, int componentPosition)
        {
            string str = null;
            string[] strArray2 = StringManager.Split(licenseKey, new string[] { LicenseKeyConstants.stDODELIM });
            if (strArray2.Length < (componentPosition + 1))
            {
                return null;
            }
            str = strArray2[componentPosition];
            return StringManager.Split(str, new string[] { LicenseKeyConstants.stCOLON })[1];
        }

        public LicenseKeyConstants.LicenseKeyType RtrvLicenseType(string stLicenseKey)
        {
            string str = this.rtrvLicenseComponent(stLicenseKey, LicenseKeyConstants.iKEY_LICENSE_TYPE);
            if (string.IsNullOrEmpty(str))
            {
                return LicenseKeyConstants.LicenseKeyType.None;
            }
            return (LicenseKeyConstants.LicenseKeyType) Enum.Parse(typeof(LicenseKeyConstants.LicenseKeyType), str, true);
        }

        public string rtrvMachineName(Session session, string stLicenseKey)
        {
            return this.rtrvLicenseComponent(stLicenseKey, LicenseKeyConstants.iKEY_MACHINE_NAME);
        }

        public string rtrvModules(Session session, string stLicenseKey)
        {
            string str = null;
            str = this.rtrvLicenseComponent(stLicenseKey, LicenseKeyConstants.iKEY_MODULES);
            if (StringManager.IsEmpty(str))
            {
                return null;
            }
            return str;
        }

        public int RtrvNumberOfUsers(string stLicenseKey)
        {
            return Convert.ToInt32(this.rtrvLicenseComponent(stLicenseKey, LicenseKeyConstants.iKEY_NUMBER_OF_USERS));
        }
    }
}

